<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>头歌 - 机器学习 - 随机森林 | David 的博客</title><meta name="author" content="David"><meta name="copyright" content="David"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="【educoder】 机器学习 --- 随机森林第1关：Bagging任务描述本关任务：补充 python 代码，完成 BaggingClassifier 类中的 fit 和 predict 函数。请不要修改 Begin-End 段之外的代码。 相关知识为了完成本关任务，你需要掌握：  什么是 Bagging； Bagging 算法如何训练与预测。  什么是Bagging Bagging 是 Bo">
<meta property="og:type" content="article">
<meta property="og:title" content="头歌 - 机器学习 - 随机森林">
<meta property="og:url" content="https://blog.david-deng.cn/2023/05/26/educoder-ml-homework/index.html">
<meta property="og:site_name" content="David 的博客">
<meta property="og:description" content="【educoder】 机器学习 --- 随机森林第1关：Bagging任务描述本关任务：补充 python 代码，完成 BaggingClassifier 类中的 fit 和 predict 函数。请不要修改 Begin-End 段之外的代码。 相关知识为了完成本关任务，你需要掌握：  什么是 Bagging； Bagging 算法如何训练与预测。  什么是Bagging Bagging 是 Bo">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-8.png">
<meta property="article:published_time" content="2023-05-26T14:11:56.000Z">
<meta property="article:modified_time" content="2023-11-16T13:33:46.000Z">
<meta property="article:author" content="David">
<meta property="article:tag" content="Educoder">
<meta property="article:tag" content="ML">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-8.png"><link rel="shortcut icon" href="/img/favicon.svg"><link rel="canonical" href="https://blog.david-deng.cn/2023/05/26/educoder-ml-homework/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><script>
    (() => {
      
    const saveToLocal = {
      set: (key, value, ttl) => {
        if (!ttl) return
        const expiry = Date.now() + ttl * 86400000
        localStorage.setItem(key, JSON.stringify({ value, expiry }))
      },
      get: key => {
        const itemStr = localStorage.getItem(key)
        if (!itemStr) return undefined
        const { value, expiry } = JSON.parse(itemStr)
        if (Date.now() > expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return value
      }
    }

    window.btf = {
      saveToLocal,
      getScript: (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        Object.entries(attr).forEach(([key, val]) => script.setAttribute(key, val))
        script.onload = script.onreadystatechange = () => {
          if (!script.readyState || /loaded|complete/.test(script.readyState)) resolve()
        }
        script.onerror = reject
        document.head.appendChild(script)
      }),
      getCSS: (url, id) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onload = link.onreadystatechange = () => {
          if (!link.readyState || /loaded|complete/.test(link.readyState)) resolve()
        }
        link.onerror = reject
        document.head.appendChild(link)
      }),
      addGlobalFn: (key, fn, name = false, parent = window) => {
        if (!false && key.startsWith('pjax')) return
        const globalFn = parent.globalFn || {}
        globalFn[key] = globalFn[key] || {}
        globalFn[key][name || Object.keys(globalFn[key]).length] = fn
        parent.globalFn = globalFn
      }
    }
  
      
      const activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      const activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }

      btf.activateDarkMode = activateDarkMode
      btf.activateLightMode = activateLightMode

      const theme = saveToLocal.get('theme')
    
          const mediaQueryDark = window.matchMedia('(prefers-color-scheme: dark)')
          const mediaQueryLight = window.matchMedia('(prefers-color-scheme: light)')
          
          if (theme === undefined) {
            if (mediaQueryLight.matches) activateLightMode()
            else if (mediaQueryDark.matches) activateDarkMode()
            else {
              const hour = new Date().getHours()
              const isNight = hour <= 6 || hour >= 18
              isNight ? activateDarkMode() : activateLightMode()
            }
            mediaQueryDark.addEventListener('change', () => {
              if (saveToLocal.get('theme') === undefined) {
                e.matches ? activateDarkMode() : activateLightMode()
              }
            })
          } else {
            theme === 'light' ? activateLightMode() : activateDarkMode()
          }
        
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        document.documentElement.classList.toggle('hide-aside', asideStatus === 'hide')
      }
    
      
    const detectApple = () => {
      if (/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)) {
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
  
    })()
  </script><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false,"highlightFullpage":false,"highlightMacStyle":false},
  copy: {
    success: '复制成功',
    error: '复制失败',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: David","link":"链接: ","source":"来源: David 的博客","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'null',
  Snackbar: {"chs_to_cht":"已切换为繁体中文","cht_to_chs":"已切换为简体中文","day_to_night":"已切换为深色模式","night_to_day":"已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#121212","position":"bottom-left"},
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: true,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '头歌 - 机器学习 - 随机森林',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  isShuoshuo: false
}</script><style type="text/css">#toggle-sidebar {bottom: 80px}</style><meta name="generator" content="Hexo 7.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img text-center"><img src="/img/avatar.png" onerror="onerror=null;src='/img/loading.gif'" alt="avatar"/></div><div class="site-data text-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">27</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">28</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">28</div></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><span class="site-page group"><i class="fa-fw fas fa-compass"></i><span> 目录</span><i class="fas fa-chevron-down"></i></span><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url(https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-8.png);"><nav id="nav"><span id="blog-info"><a class="nav-site-title" href="/"><span class="site-name">David 的博客</span></a><a class="nav-page-title" href="/"><span class="site-name">头歌 - 机器学习 - 随机森林</span></a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><span class="site-page group"><i class="fa-fw fas fa-compass"></i><span> 目录</span><i class="fas fa-chevron-down"></i></span><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><span class="site-page"><i class="fas fa-bars fa-fw"></i></span></div></div></nav><div id="post-info"><h1 class="post-title">头歌 - 机器学习 - 随机森林</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-05-26T14:11:56.000Z" title="发表于 2023-05-26 22:11:56">2023-05-26</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-11-16T13:33:46.000Z" title="更新于 2023-11-16 21:33:46">2023-11-16</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Educoder/">Educoder</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Educoder/ML/">ML</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">总字数:</span><span class="word-count">3.4k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>12分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title=""><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">浏览量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="container post-content" id="article-container"><div id="post-outdate-notice" data="{&quot;limitDay&quot;:90,&quot;messagePrev&quot;:&quot;文章距离最近一次更新已经&quot;,&quot;messageNext&quot;:&quot;天，文章的内容可能已经过期。&quot;,&quot;postUpdate&quot;:&quot;2023-11-16 21:33:46&quot;}" hidden></div><h1 id="【educoder】-机器学习-随机森林"><a href="#【educoder】-机器学习-随机森林" class="headerlink" title="【educoder】 机器学习 --- 随机森林"></a>【educoder】 机器学习 --- 随机森林</h1><h2 id="第1关：Bagging"><a href="#第1关：Bagging" class="headerlink" title="第1关：Bagging"></a>第1关：Bagging</h2><h3 id="任务描述"><a href="#任务描述" class="headerlink" title="任务描述"></a>任务描述</h3><p>本关任务：补充 python 代码，完成 BaggingClassifier 类中的 fit 和 predict 函数。<strong>请不要修改 Begin-End 段之外的代码。</strong></p>
<h3 id="相关知识"><a href="#相关知识" class="headerlink" title="相关知识"></a>相关知识</h3><p>为了完成本关任务，你需要掌握：</p>
<ul>
<li>什么是 Bagging；</li>
<li>Bagging 算法如何训练与预测。</li>
</ul>
<h4 id="什么是Bagging"><a href="#什么是Bagging" class="headerlink" title="什么是Bagging"></a>什么是Bagging</h4><p> Bagging 是 Bootstrap Aggregating 的英文缩写，刚接触的童鞋不要误认为 Bagging 是一种算法， Bagging 和 Boosting 都是集成学习中的学习框架，代表着不同的思想。与 Boosting 这种串行集成学习算法不同， Bagging 是并行式集成学习方法。大名鼎鼎的随机森林算法就是在 Bagging 的基础上修改的算法。</p>
<p><strong>Bagging 方法的核心思想就是三个臭皮匠顶个诸葛亮</strong>。如果使用 Bagging 解决分类问题，就是将多个分类器的结果整合起来进行投票，选取票数最高的结果作为最终结果。如果使用 Bagging 解决回归问题，就将多个回归器的结果加起来然后求平均，将平均值作为最终结果。</p>
<p>那么 Bagging 方法如此有效呢，举个例子。狼人杀我相信大家都玩过，在天黑之前，村民们都要根据当天所发生的事和别人的发现来投票决定谁可能是狼人。</p>
<p>如果我们将每个村民看成是一个分类器，那么每个村民的任务就是二分类，假设<em>h**i</em>(<em>x</em>)表示第 i 个村民认为 x 是不是狼人( -1 代表不是狼人，1 代表是狼人)，<em>f</em>(<em>x</em>)表示 x 真正的身份(是不是狼人)，<em>ϵ</em>表示为村民判断错误的错误率。则有<em>P</em>(<em>h**i</em>(<em>x</em>)&#x3D;<em>f</em>(<em>x</em>))&#x3D;<em>ϵ</em>。</p>
<p>根据狼人杀的规则，村民们需要投票决定天黑前谁是狼人，也就是说如果有超过半数的村民投票时猜对了，那么这一轮就猜对了。那么假设现在有<em>T</em>个村民，<em>H</em>(<em>x</em>)表示投票后最终的结果，则有<em>H</em>(<em>x</em>)&#x3D;<em>s<strong>i</strong>g**n</em>(∑<em>i</em>&#x3D;1<em>T<strong>h</strong>i</em>(<em>x</em>))。</p>
<p>现在假设每个村民都是有主见的人，对于谁是狼人都有自己的想法，那么他们的错误率也是相互独立的。那么根据 Hoeffding不等式 可知，<em>H</em>(<em>x</em>)的错误率为：</p>
<p><em>P</em>(<em>H</em>(<em>x</em>)&#x3D;<em>f</em>(<em>x</em>))&#x3D;<em>k</em>&#x3D;0∑<em>T</em>&#x2F;2<em>C<strong>T</strong>k</em>(1−<em>ϵ</em>)<em>k<strong>ϵ</strong>T</em>−<em>k</em>≤<em>e<strong>x</strong>p</em>(−21<em>T</em>(1−2<em>ϵ</em>)2)</p>
<p>根据上式可知，如果 5 个村民，每个村民的错误率为 0.33，那么投票的错误率为 0.749；如果 20 个村民，每个村民的错误率为 0.33，那么投票的错误率为 0.315；如果 50 个村民，每个村民的错误率为 0.33，那么投票的错误率为 0.056；如果 100 个村民，每个村民的错误率为 0.33，那么投票的错误率为 0.003。**从结果可以看出，村民的数量越大，那么投票后犯错的错误率就越小。**这也是 Bagging 性能强的原因之一。</p>
<h4 id="Bagging方法如何训练与预测"><a href="#Bagging方法如何训练与预测" class="headerlink" title="Bagging方法如何训练与预测"></a>Bagging方法如何训练与预测</h4><h4 id="训练"><a href="#训练" class="headerlink" title="训练"></a>训练</h4><p> Bagging 在训练时的特点就是<strong>随机有放回采样</strong>和<strong>并行</strong>。</p>
<ul>
<li>**随机有放回采样：**假设训练数据集有 m 条样本数据，每次从这 m 条数据中随机取一条数据放入采样集，然后将其返回，让下一次采样有机会仍然能被采样。然后重复 m 次，就能得到拥有 m 条数据的采样集，该采样集作为 Bagging 的众多分类器中的一个作为训练数据集。假设有 T 个分类器（随便什么分类器），那么就重复 T 此随机有放回采样，构建出 T 个采样集分别作为 T 个分类器的训练数据集。</li>
<li>**并行：**假设有 10 个分类器，在 Boosting 中，1 号分类器训练完成之后才能开始 2 号分类器的训练，而在 Bagging 中，分类器可以同时进行训练，当所有分类器训练完成之后，整个 Bagging 的训练过程就结束了。</li>
</ul>
<p> Bagging 训练过程如下图所示：</p>
<p><img src="https://cdn.jsdelivr.net/gh/David-deng-01/images/blog294338" alt="img"> </p>
<h4 id="预测"><a href="#预测" class="headerlink" title="预测"></a>预测</h4><p> Bagging 在预测时非常简单，就是<strong>投票</strong>！比如现在有 5 个分类器，有 3 个分类器认为当前样本属于 A 类，1 个分类器认为属于 B 类，1 个分类器认为属于 C 类，那么 Bagging 的结果会是 A 类（因为 A 类的票数最高）。</p>
<p> Bagging 预测过程如下图所示:</p>
<p><img src="https://cdn.jsdelivr.net/gh/David-deng-01/images/blog294341" alt="img"> </p>
<h3 id="编程要求"><a href="#编程要求" class="headerlink" title="编程要求"></a>编程要求</h3><p>在 begin-end 中完成 BaggingClassifier 类中的 fit 和 predict 函数。分类器可使用 sklearn 提供的 DecisionTreeClassifier。要求模型保存在 self.models 中。</p>
<p> fit 函数用于 Bagging 的训练过程，其中：</p>
<ul>
<li>feature ：训练集数据，类型为 ndarray；</li>
<li>label ：训练集标签，类型为 ndarray。</li>
</ul>
<p> predict 函数，实现预测功能，并将标签返回，其中：</p>
<ul>
<li>feature ：测试集数据，类型为 ndarray 。<strong>（PS：feature中有多条数据）</strong></li>
</ul>
<h3 id="测试说明"><a href="#测试说明" class="headerlink" title="测试说明"></a>测试说明</h3><p>只需完成 fit 与 predict 函数即可，程序内部会调用您所完成的 fit 函数构建模型并调用 predict 函数来对数据进行预测。预测的准确率高于 0.9 视为过关。</p>
<h3 id="参考答案"><a href="#参考答案" class="headerlink" title="参考答案"></a>参考答案</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">from</span> collections <span class="keyword">import</span> Counter</span><br><span class="line"><span class="keyword">from</span> sklearn.tree <span class="keyword">import</span> DecisionTreeClassifier</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">BaggingClassifier</span>():</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self, n_model=<span class="number">10</span></span>):</span><br><span class="line">        <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">            初始化函数</span></span><br><span class="line"><span class="string">        &quot;&quot;&quot;</span></span><br><span class="line">        <span class="comment"># 分类器的数量，默认为10</span></span><br><span class="line">        <span class="variable language_">self</span>.n_model = n_model</span><br><span class="line">        <span class="comment"># 用于保存模型的列表，训练好分类器后将对象append进去即可</span></span><br><span class="line">        <span class="variable language_">self</span>.models = []</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">fit</span>(<span class="params">self, feature, label</span>):</span><br><span class="line">        <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">            训练模型</span></span><br><span class="line"><span class="string">            :param feature: 训练数据集所有特征组成的ndarray</span></span><br><span class="line"><span class="string">            :param label:训练数据集中所有标签组成的ndarray</span></span><br><span class="line"><span class="string">            :return: None</span></span><br><span class="line"><span class="string">        &quot;&quot;&quot;</span></span><br><span class="line">        <span class="comment"># ************* Begin ************#</span></span><br><span class="line">        <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="variable language_">self</span>.n_model):</span><br><span class="line">            m = <span class="built_in">len</span>(feature)</span><br><span class="line">            index = np.random.choice(m, m)</span><br><span class="line">            sample_data = feature[index]</span><br><span class="line">            sample_lable = label[index]</span><br><span class="line">            model = DecisionTreeClassifier()</span><br><span class="line">            model = model.fit(sample_data, sample_lable)</span><br><span class="line">            <span class="variable language_">self</span>.models.append(model)</span><br><span class="line">        <span class="comment"># ************* End **************#</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">predict</span>(<span class="params">self, feature</span>):</span><br><span class="line">        <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">            :param feature:训练数据集所有特征组成的ndarray</span></span><br><span class="line"><span class="string">            :return:预测结果，如np.array([0, 1, 2, 2, 1, 0])</span></span><br><span class="line"><span class="string">        &quot;&quot;&quot;</span></span><br><span class="line">        <span class="comment"># ************* Begin ************#</span></span><br><span class="line">        result = []</span><br><span class="line">        vote = []</span><br><span class="line">        <span class="keyword">for</span> model <span class="keyword">in</span> <span class="variable language_">self</span>.models:</span><br><span class="line">            r = model.predict(feature)</span><br><span class="line">            vote.append(r)</span><br><span class="line">        vote = np.array(vote)</span><br><span class="line">        <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="built_in">len</span>(feature)):</span><br><span class="line">            v = <span class="built_in">sorted</span>(Counter(vote[:, i]).items(), key=<span class="keyword">lambda</span> x: x[<span class="number">1</span>], reverse=<span class="literal">True</span>)</span><br><span class="line">            result.append(v[<span class="number">0</span>][<span class="number">0</span>])</span><br><span class="line">        <span class="keyword">return</span> np.array(result)</span><br><span class="line">        <span class="comment"># ************* End **************#</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="第2关：随机森林算法流程"><a href="#第2关：随机森林算法流程" class="headerlink" title="第2关：随机森林算法流程"></a>第2关：随机森林算法流程</h2><h3 id="任务描述-1"><a href="#任务描述-1" class="headerlink" title="任务描述"></a>任务描述</h3><p>本关任务：补充 python 代码，完成 RandomForestClassifier 类中的 fit 和 predict 函数。<strong>请不要修改 Begin-End 段之外的代码。</strong></p>
<h3 id="相关知识-1"><a href="#相关知识-1" class="headerlink" title="相关知识"></a>相关知识</h3><p>为了完成本关任务，你需要掌握随机森林的训练与预测流程</p>
<h4 id="随机森林的训练流程"><a href="#随机森林的训练流程" class="headerlink" title="随机森林的训练流程"></a>随机森林的训练流程</h4><p>随机森林是 Bagging 的一种扩展变体，随机森林的训练过程相对与 Bagging 的训练过程的改变有：</p>
<ul>
<li>基学习器： Bagging 的基学习器可以是<strong>任意学习器</strong>，而随机森林则是以<strong>决策树作为基学习器</strong>。</li>
<li>随机属性选择：假设原始训练数据集有 10 个特征，从这 10 个特征中随机选取 k 个特征构成训练数据子集，然后将这个子集作为训练集扔给决策树去训练。其中 k 的取值一般为 log2(特征数量) 。</li>
</ul>
<p>这样的改动通常会使得<strong>随机森林具有更加强的泛化性</strong>，因为每一棵决策树的训练数据集是随机的，而且训练数据集中的特征也是随机抽取的。如果每一棵决策树模型的差异比较大，那么就很容易能够解决决策树容易过拟合的问题。</p>
<p>随机森林训练过程伪代码如下：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#假设数据集为D，标签集为A，需要构造的决策树为tree</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">fit</span>(<span class="params">D, A</span>):</span><br><span class="line">    models = []</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(决策树的数量):</span><br><span class="line">        有放回的随机采样数据，得到数据集sample_D和标签sample_A</span><br><span class="line">        从采样到的数据中随机抽取K个特征构成训练集sub_D</span><br><span class="line">        构建决策树tree</span><br><span class="line">        tree.fit(sub_D, sample_A)</span><br><span class="line">        models.append(tree)</span><br><span class="line">    <span class="keyword">return</span> models</span><br></pre></td></tr></table></figure>

<h4 id="随机森林的预测流程"><a href="#随机森林的预测流程" class="headerlink" title="随机森林的预测流程"></a>随机森林的预测流程</h4><p>随机森林的预测流程与 Bagging 的预测流程基本一致，如果是回归，就将结果基学习器的预测结果全部加起来算平均；如果是分类，就投票，票数最多的结果作为最终结果。**但需要注意的是，在预测时所用到的特征必须与训练模型时所用到的特征保持一致。**例如，第 3 棵决策树在训练时用到了训练集的第 2，5，8 这 3 个特征。那么在预测时也要用第 2，5，8 这 3 个特征所组成的测试集传给第 3 棵决策树进行预测。</p>
<h3 id="编程要求-1"><a href="#编程要求-1" class="headerlink" title="编程要求"></a>编程要求</h3><p>在 begin-end 中完成 RandomForestClassifier 类中的 fit 和 predict 函数。分类器可使用 sklearn 提供的 DecisionTreeClassifier ，要求模型保存在 self.models 中。</p>
<p> fit 函数用于随机森林的训练过程，其中：</p>
<ul>
<li>feature ：训练集数据，类型为 ndarray；</li>
<li>label ：训练集标签，类型为 ndarray。</li>
</ul>
<p> predict 函数，实现预测功能，并将标签返回，其中：</p>
<ul>
<li>feature ：测试集数据，类型为 ndarray 。<strong>（PS：feature中有多条数据）</strong></li>
</ul>
<h3 id="测试说明-1"><a href="#测试说明-1" class="headerlink" title="测试说明"></a>测试说明</h3><p>只需完成 fit 与 predict 函数即可，程序内部会调用您所完成的 fit 函数构建模型并调用 predict 函数来对数据进行预测。预测的准确率高于 0.9 视为过关。</p>
<h3 id="参考答案-1"><a href="#参考答案-1" class="headerlink" title="参考答案"></a>参考答案</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">from</span> collections <span class="keyword">import</span> Counter</span><br><span class="line"><span class="keyword">from</span> sklearn.tree <span class="keyword">import</span> DecisionTreeClassifier</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">RandomForestClassifier</span>():</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self, n_model=<span class="number">10</span></span>):</span><br><span class="line">        <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">            初始化函数</span></span><br><span class="line"><span class="string">        &quot;&quot;&quot;</span></span><br><span class="line">        <span class="comment"># 分类器的数量，默认为10</span></span><br><span class="line">        <span class="variable language_">self</span>.n_model = n_model</span><br><span class="line">        <span class="comment"># 用于保存模型的列表，训练好分类器后将对象append进去即可</span></span><br><span class="line">        <span class="variable language_">self</span>.models = []</span><br><span class="line">        <span class="comment"># 用于保存决策树训练时随机选取的列的索引</span></span><br><span class="line">        <span class="variable language_">self</span>.col_indexs = []</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">fit</span>(<span class="params">self, feature, label</span>):</span><br><span class="line">        <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">            训练模型</span></span><br><span class="line"><span class="string">            :param feature: 训练数据集所有特征组成的ndarray</span></span><br><span class="line"><span class="string">            :param label:训练数据集中所有标签组成的ndarray</span></span><br><span class="line"><span class="string">            :return: None</span></span><br><span class="line"><span class="string">        &quot;&quot;&quot;</span></span><br><span class="line">        <span class="comment"># ************* Begin ************#</span></span><br><span class="line">        <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="variable language_">self</span>.n_model):</span><br><span class="line">            m = <span class="built_in">len</span>(feature)</span><br><span class="line">            index = np.random.choice(m, m)</span><br><span class="line">            col_index = np.random.permutation(<span class="built_in">len</span>(feature[<span class="number">0</span>]))[:<span class="built_in">int</span>(np.log2(<span class="built_in">len</span>(feature[<span class="number">0</span>])))]</span><br><span class="line">            sample_data = feature[index]</span><br><span class="line">            sample_data = sample_data[:, col_index]</span><br><span class="line">            sample_lable = label[index]</span><br><span class="line">            model = DecisionTreeClassifier()</span><br><span class="line">            model = model.fit(sample_data, sample_lable)</span><br><span class="line">            <span class="variable language_">self</span>.models.append(model)</span><br><span class="line">            <span class="variable language_">self</span>.col_indexs.append(col_index)</span><br><span class="line">        <span class="comment"># ************* End **************#</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">predict</span>(<span class="params">self, feature</span>):</span><br><span class="line">        <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">            :param feature:训练数据集所有特征组成的ndarray</span></span><br><span class="line"><span class="string">            :return:预测结果，如np.array([0, 1, 2, 2, 1, 0])</span></span><br><span class="line"><span class="string">        &quot;&quot;&quot;</span></span><br><span class="line">        <span class="comment"># ************* Begin ************#</span></span><br><span class="line">        result = []</span><br><span class="line">        vote = []</span><br><span class="line">        <span class="keyword">for</span> i, model <span class="keyword">in</span> <span class="built_in">enumerate</span>(<span class="variable language_">self</span>.models):</span><br><span class="line">            f = feature[:, <span class="variable language_">self</span>.col_indexs[i]]</span><br><span class="line">            r = model.predict(f)</span><br><span class="line">            vote.append(r)</span><br><span class="line">        vote = np.array(vote)</span><br><span class="line">        <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="built_in">len</span>(feature)):</span><br><span class="line">            v = <span class="built_in">sorted</span>(Counter(vote[:, i]).items(), key=<span class="keyword">lambda</span> x: x[<span class="number">1</span>], reverse=<span class="literal">True</span>)</span><br><span class="line">            result.append(v[<span class="number">0</span>][<span class="number">0</span>])</span><br><span class="line">        <span class="keyword">return</span> np.array(result)</span><br><span class="line">        <span class="comment"># ************* End **************#</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="第3关：手写数字识别"><a href="#第3关：手写数字识别" class="headerlink" title="第3关：手写数字识别"></a>第3关：手写数字识别</h2><h3 id="任务描述-2"><a href="#任务描述-2" class="headerlink" title="任务描述"></a>任务描述</h3><p>本关任务：使用 sklearn 中的 RandomForestClassifier 类完成手写数字识别任务。<strong>请不要修改Begin-End段之外的代码。</strong></p>
<h3 id="相关知识-2"><a href="#相关知识-2" class="headerlink" title="相关知识"></a>相关知识</h3><p>为了完成本关任务，你需要掌握如何使用 sklearn 提供的 RandomForestClassifier 类。</p>
<h4 id="数据简介"><a href="#数据简介" class="headerlink" title="数据简介"></a>数据简介</h4><p>本关使用的是手写数字数据集，该数据集有 1797 个样本，每个样本包括 8*8 像素（实际上是一条样本有 64 个特征，每个像素看成是一个特征，每个特征都是 float 类型的数值）的图像和一个 [0, 9] 整数的标签。比如下图的标签是 2 ：</p>
<p><img src="https://cdn.jsdelivr.net/gh/David-deng-01/images/blog294852" alt="img"> </p>
<h4 id="RandomForestClassifier"><a href="#RandomForestClassifier" class="headerlink" title="RandomForestClassifier"></a>RandomForestClassifier</h4><p> RandomForestClassifier 的构造函数中有两个常用的参数可以设置：</p>
<ul>
<li>n_estimators ：森林中决策树的数量；</li>
<li>criterion ：构建决策树时，划分节点时用到的指标。有 gini （<strong>基尼系数</strong>）, entropy (<strong>信息增益</strong>)。若不设置，默认为 gini；</li>
<li>max_depth ：决策树的最大深度，如果发现模型已经出现过拟合，可以尝试将该参数调小。若不设置，默认为 None；</li>
<li>max_features ：随机选取特征时选取特征的数量，一般传入 auto 或者 log2，默认为 auto ， auto 表示 max_features&#x3D;sqrt(训练集中特征的数量) ；log2 表示 max_features&#x3D;log2(训练集中特征的数量)。</li>
</ul>
<p> RandomForestClassifier 类中的 fit 函数实现了随机森林分类器训练模型的功能，predict 函数实现了模型预测的功能。</p>
<p>其中 fit 函数的参数如下：</p>
<ul>
<li>X ：大小为 [样本数量,特征数量] 的 ndarry，存放训练样本；</li>
<li>Y ：值为整型，大小为 [样本数量] 的 ndarray，存放训练样本的分类标签。</li>
</ul>
<p>而 predict 函数有一个向量输入：</p>
<ul>
<li>X ：大小为 [样本数量,特征数量] 的 ndarry，存放预测样本。</li>
</ul>
<p> RandomForestClassifier 的使用代码如下：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> sklearn.ensemble <span class="keyword">import</span> RandomForestClassifier</span><br><span class="line"></span><br><span class="line">clf = RandomForestClassifier(n_estimators=<span class="number">50</span>)</span><br><span class="line">clf.fit(X_train, Y_train)</span><br><span class="line">result = clf.predict(X_test)</span><br></pre></td></tr></table></figure>

<h3 id="编程要求-2"><a href="#编程要求-2" class="headerlink" title="编程要求"></a>编程要求</h3><p>在右侧区域的 begin-end 之间填写<code>digit_predict(train_image, train_label, test_image)</code>函数完成手写数字分类任务，其中：</p>
<ul>
<li>train_image ：包含多条训练样本的样本集，类型为 ndarray ， shape 为 [-1, 8, 8] ，<strong>在喂给分类器之前请记得将其变形</strong>；</li>
<li>train_label ：包含多条训练样本标签的标签集，类型为 ndarray；</li>
<li>test_image ：包含多条测试样本的测试集，类型为 ndarray；</li>
<li>return ： test_image 对应的预测标签，类型为 ndarray。</li>
</ul>
<h3 id="测试说明-2"><a href="#测试说明-2" class="headerlink" title="测试说明"></a>测试说明</h3><p>只需完成 digit_predict 函数即可，程序内部会检测您的代码，预测正确率高于 0.98 视为过关。</p>
<h3 id="参考答案-2"><a href="#参考答案-2" class="headerlink" title="参考答案"></a>参考答案</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> sklearn.ensemble <span class="keyword">import</span> RandomForestClassifier</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">digit_predict</span>(<span class="params">train_image, train_label, test_image</span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">        实现功能：训练模型并输出预测结果</span></span><br><span class="line"><span class="string">        :param train_image: 包含多条训练样本的样本集，类型为ndarray,shape为[-1, 8, 8]</span></span><br><span class="line"><span class="string">        :param train_label: 包含多条训练样本标签的标签集，类型为ndarray</span></span><br><span class="line"><span class="string">        :param test_image: 包含多条测试样本的测试集，类型为ndarry</span></span><br><span class="line"><span class="string">        :return: test_image对应的预测标签，类型为ndarray</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># ************* Begin ************#</span></span><br><span class="line">    X = np.reshape(train_image, newshape=(-<span class="number">1</span>, <span class="number">64</span>))</span><br><span class="line">    clf = RandomForestClassifier(n_estimators=<span class="number">500</span>, max_depth=<span class="number">10</span>)</span><br><span class="line">    clf.fit(X, y=train_label)</span><br><span class="line">    <span class="keyword">return</span> clf.predict(test_image)</span><br><span class="line">    <span class="comment"># ************* End **************#</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://blog.david-deng.cn">David</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://blog.david-deng.cn/2023/05/26/educoder-ml-homework/">https://blog.david-deng.cn/2023/05/26/educoder-ml-homework/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="external nofollow noreferrer" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来源 <a href="https://blog.david-deng.cn" target="_blank">David 的博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Educoder/">Educoder</a><a class="post-meta__tags" href="/tags/ML/">ML</a></div><div class="post-share"><div class="social-share" data-image="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-8.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i>赞助</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="https://cdn.jsdelivr.net/gh/David-deng-01/images/blog/WeChatPay.jpg" rel="external nofollow noreferrer" target="_blank"><img class="post-qr-code-img" src="https://cdn.jsdelivr.net/gh/David-deng-01/images/blog/WeChatPay.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="https://cdn.jsdelivr.net/gh/David-deng-01/images/blog/Alipay.jpg" rel="external nofollow noreferrer" target="_blank"><img class="post-qr-code-img" src="https://cdn.jsdelivr.net/gh/David-deng-01/images/blog/Alipay.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><nav class="pagination-post" id="pagination"><a class="pagination-related" href="/2023/05/25/educoder-ml-homework/" title="头歌 - 机器学习 - Adaboost"><img class="cover" src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-9.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="info"><div class="info-1"><div class="info-item-1">上一篇</div><div class="info-item-2">头歌 - 机器学习 - Adaboost</div></div><div class="info-2"><div class="info-item-1">【educoder】 机器学习 --- Adaboost第1关：Boosting任务描述本关任务：根据本节课所学知识完成本关所设置的选择题。 相关知识为了完成本关任务，你需要掌握：1.什么是集成学习，2.Boosting。 什么是集成学习集成学习方法是一种常用的机器学习方法，分为 bagging 与 boosting 两种方法，应用十分广泛。集成学习基本思想是：对于一个复杂的学习任务，我们首先构造多个简单的学习模型，然后再把这些简单模型组合成一个高效的学习模型。实际上，就是**“三个臭皮匠顶个诸葛亮”**的道理。   集成学习采取投票的方式来综合多个简单模型的结果，按 bagging 投票思想，如下面例子：   假设一共训练了 5 个简单模型，每个模型对分类结果预测如上图，则最终预测结果为： A:2  B:3  3&gt;2  结果为 B 不过在有的时候，每个模型对分类结果的确定性不一样，即有的对分类结果非常肯定，有的不是很肯定,说明每个模型投的一票应该是有相应的权重来衡量这一票的重要性。就像在歌手比赛中，每个观众投的票记 1 分，而专家投票记 10 分。按 boosting...</div></div></div></a><a class="pagination-related" href="/2023/08/30/%E7%A0%94%E4%BA%8C%E5%AD%A6%E5%B9%B4%E8%AE%A1%E5%88%92%20&amp;%20%E5%AE%89%E6%8E%92/" title="研二学年计划 &amp; 安排"><img class="cover" src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-6.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="info text-right"><div class="info-1"><div class="info-item-1">下一篇</div><div class="info-item-2">研二学年计划 & 安排</div></div><div class="info-2"><div class="info-item-1">研二学年计划 &amp; 安排研二学年计划 —— 想干啥？  通过大学英语六级  发文章  写博客  入党  研二学年安排 —— 怎么干？通过大学英语六级 背单词，坚持每天 背单词 ！！！ 学语法，学习基础的英语语法 练习听力，阅读，写作，主要是阅读的速度和写作的能力  发文章目前正在做的一些工作如下：  吐槽大会数据集 讽刺生成（强化学习） 文本风格转换综述 中药 VQA（数据集 + 模型）  目前各个项目的进展如下（截止到 2023.08.30）：  吐槽大会数据集正在进行字幕校对的工作，后续工作主要是完成字幕校对，进行情感标注，撰写文章和文章修改 讽刺生成目前正在可行性验证阶段，后续的工作主要是完成模型的构建，撰写文章和文章修改 文本风格转换数据集正在进行论文收集和阅读的工作，后续的工作主要是进行研究方向的工作总结，撰写文章和文章修改 中药 VQA 数据集已经基本构建完成，目前正在进行的是基线模型的复现，后续工作主要是将基线模型使用在我们的数据集中，然后构建自己的用于 VQA...</div></div></div></a></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><a class="pagination-related" href="/2023/05/04/educoder-ml-homework/" title="头歌 - 机器学习 - 决策树"><img class="cover" src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-2.png" alt="cover"><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2023-05-04</div><div class="info-item-2">头歌 - 机器学习 - 决策树</div></div><div class="info-2"><div class="info-item-1">【educoder】 机器学习 --- 决策树第 1...</div></div></div></a><a class="pagination-related" href="/2023/05/09/Linear-Regression-And-Logistic-Regression/" title="线性回归和逻辑回归"><img class="cover" src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-1.png" alt="cover"><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2023-05-09</div><div class="info-item-2">线性回归和逻辑回归</div></div><div class="info-2"><div class="info-item-1">线性回归和逻辑回归1. 线性回归(Linear Regression)什么是回归（Regression）回归分析是一种统计方法,于对具有一个或多个自变量的因变量(目标变量)和自变量(预测变量)之间的关系进行建模。具体地说，回归分析有助于我们理解在其他自变量保持固定的情况下，自变量的值对应于自变量的变化方式。它可以预测连续&#x2F;实际值,例如温度,年龄,工资,价格等。 通俗理解：越来越接近期望值的过程，回归于事物本来的面目。 回归是一种有监督的学习技术，有助于发现变量之间的相关性,并使我们能够基于一个或多个预测变量来预测连续输出变量。它主要用于预测，时间序列建模以及确定变量之间的因果关系。 在回归中，我们在最适合给定数据点的变量之间绘制图形,使用此图形，机器学习模型可以对数据进行预测。用简单的话说，回归显示一条线或曲线， 它穿过目标预测图上的所有数据点，以使数据点和回归线之间的垂直距离最小。数据点和线之间的距离表明模型是否已捕获牢固的关系。 主要用于预测数值型数据，典型的回归例子：数据拟合曲线 什么是线性回归（Linear...</div></div></div></a><a class="pagination-related" href="/2023/05/11/educoder-ml-homework/" title="头歌 - 机器学习 - 逻辑回归"><img class="cover" src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-5.png" alt="cover"><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2023-05-11</div><div class="info-item-2">头歌 - 机器学习 - 逻辑回归</div></div><div class="info-2"><div class="info-item-1">【educoder】 机器学习 --- 逻辑回归第 1 关：逻辑回归核心思想任务描述本关任务：根据本节课所学知识完成本关所设置的编程题。 相关知识为了完成本关任务，你需要掌握：  什么是逻辑回归； sigmoid函数。  什么是逻辑回归当一看到“回归”这两个字，可能会认为逻辑回归是一种解决回归问题的算法，然而逻辑回归是通过回归的思想来解决二分类问题的算法。 那么问题来了，回归的算法怎样解决分类问题呢？其实很简单，逻辑回归是将样本特征和样本所属类别的概率联系在一起，假设现在已经训练好了一个逻辑回归的模型为 f(x) ，模型的输出是样本 x 的标签是 1 的概率，则该模型可以表示，$\widehat{p}&#x3D;f(x)$ 。若得到了样本 x 属于标签 1 的概率后，很自然的就能想到当$\widehat{p}&gt;0.5$ 时 x 属于标签 1 ，否则属于标签 0 。所以就有 $$\widehat{y} &#x3D;\begin{cases}0, &amp; \widehat{p}&lt;0.5 \[2ex]1, &amp; \widehat{p}&gt;0.5...</div></div></div></a><a class="pagination-related" href="/2023/05/25/educoder-ml-homework/" title="头歌 - 机器学习 - Adaboost"><img class="cover" src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-9.png" alt="cover"><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2023-05-25</div><div class="info-item-2">头歌 - 机器学习 - Adaboost</div></div><div class="info-2"><div class="info-item-1">【educoder】 机器学习 --- Adaboost第1关：Boosting任务描述本关任务：根据本节课所学知识完成本关所设置的选择题。 相关知识为了完成本关任务，你需要掌握：1.什么是集成学习，2.Boosting。 什么是集成学习集成学习方法是一种常用的机器学习方法，分为 bagging 与 boosting 两种方法，应用十分广泛。集成学习基本思想是：对于一个复杂的学习任务，我们首先构造多个简单的学习模型，然后再把这些简单模型组合成一个高效的学习模型。实际上，就是**“三个臭皮匠顶个诸葛亮”**的道理。   集成学习采取投票的方式来综合多个简单模型的结果，按 bagging 投票思想，如下面例子：   假设一共训练了 5 个简单模型，每个模型对分类结果预测如上图，则最终预测结果为： A:2  B:3  3&gt;2  结果为 B 不过在有的时候，每个模型对分类结果的确定性不一样，即有的对分类结果非常肯定，有的不是很肯定,说明每个模型投的一票应该是有相应的权重来衡量这一票的重要性。就像在歌手比赛中，每个观众投的票记 1 分，而专家投票记 10 分。按 boosting...</div></div></div></a></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info text-center"><div class="avatar-img"><img src="/img/avatar.png" onerror="this.onerror=null;this.src='/img/loading.gif'" alt="avatar"/></div><div class="author-info-name">David</div><div class="author-info-description">Welcome to David's Blog</div><div class="site-data"><a href="/archives/"><div class="headline">文章</div><div class="length-num">27</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">28</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">28</div></a></div><a id="card-info-btn" target="_blank" rel="noopener external nofollow noreferrer" href="https://github.com/david-deng-01"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons"><a class="social-icon" href="https://github.com/david-deng-01" rel="external nofollow noreferrer" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="https://blog.csdn.net/David_0925" rel="external nofollow noreferrer" target="_blank" title="CSDN"><i class="fa fa-book-open"></i></a><a class="social-icon" href="tencent://AddContact/?fromId=45&amp;fromSubId=1&amp;subcmd=all&amp;uin=635647792&amp;website=www.oicqzone.com" rel="external nofollow noreferrer" target="_blank" title="QQ"><i class="fab fa-qq"></i></a><a class="social-icon" href="mailto:david-deng-0925@qq.com" rel="external nofollow noreferrer" target="_blank" title="Email"><i class="fas fa-envelope-open-text"></i></a></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E3%80%90educoder%E3%80%91-%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0-%E9%9A%8F%E6%9C%BA%E6%A3%AE%E6%9E%97"><span class="toc-number">1.</span> <span class="toc-text">【educoder】 机器学习 --- 随机森林</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AC%AC1%E5%85%B3%EF%BC%9ABagging"><span class="toc-number">1.1.</span> <span class="toc-text">第1关：Bagging</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%BB%E5%8A%A1%E6%8F%8F%E8%BF%B0"><span class="toc-number">1.1.1.</span> <span class="toc-text">任务描述</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%9B%B8%E5%85%B3%E7%9F%A5%E8%AF%86"><span class="toc-number">1.1.2.</span> <span class="toc-text">相关知识</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AFBagging"><span class="toc-number">1.1.2.1.</span> <span class="toc-text">什么是Bagging</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Bagging%E6%96%B9%E6%B3%95%E5%A6%82%E4%BD%95%E8%AE%AD%E7%BB%83%E4%B8%8E%E9%A2%84%E6%B5%8B"><span class="toc-number">1.1.2.2.</span> <span class="toc-text">Bagging方法如何训练与预测</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%AE%AD%E7%BB%83"><span class="toc-number">1.1.2.3.</span> <span class="toc-text">训练</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%A2%84%E6%B5%8B"><span class="toc-number">1.1.2.4.</span> <span class="toc-text">预测</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BC%96%E7%A8%8B%E8%A6%81%E6%B1%82"><span class="toc-number">1.1.3.</span> <span class="toc-text">编程要求</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%8B%E8%AF%95%E8%AF%B4%E6%98%8E"><span class="toc-number">1.1.4.</span> <span class="toc-text">测试说明</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%82%E8%80%83%E7%AD%94%E6%A1%88"><span class="toc-number">1.1.5.</span> <span class="toc-text">参考答案</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AC%AC2%E5%85%B3%EF%BC%9A%E9%9A%8F%E6%9C%BA%E6%A3%AE%E6%9E%97%E7%AE%97%E6%B3%95%E6%B5%81%E7%A8%8B"><span class="toc-number">1.2.</span> <span class="toc-text">第2关：随机森林算法流程</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%BB%E5%8A%A1%E6%8F%8F%E8%BF%B0-1"><span class="toc-number">1.2.1.</span> <span class="toc-text">任务描述</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%9B%B8%E5%85%B3%E7%9F%A5%E8%AF%86-1"><span class="toc-number">1.2.2.</span> <span class="toc-text">相关知识</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%9A%8F%E6%9C%BA%E6%A3%AE%E6%9E%97%E7%9A%84%E8%AE%AD%E7%BB%83%E6%B5%81%E7%A8%8B"><span class="toc-number">1.2.2.1.</span> <span class="toc-text">随机森林的训练流程</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%9A%8F%E6%9C%BA%E6%A3%AE%E6%9E%97%E7%9A%84%E9%A2%84%E6%B5%8B%E6%B5%81%E7%A8%8B"><span class="toc-number">1.2.2.2.</span> <span class="toc-text">随机森林的预测流程</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BC%96%E7%A8%8B%E8%A6%81%E6%B1%82-1"><span class="toc-number">1.2.3.</span> <span class="toc-text">编程要求</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%8B%E8%AF%95%E8%AF%B4%E6%98%8E-1"><span class="toc-number">1.2.4.</span> <span class="toc-text">测试说明</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%82%E8%80%83%E7%AD%94%E6%A1%88-1"><span class="toc-number">1.2.5.</span> <span class="toc-text">参考答案</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AC%AC3%E5%85%B3%EF%BC%9A%E6%89%8B%E5%86%99%E6%95%B0%E5%AD%97%E8%AF%86%E5%88%AB"><span class="toc-number">1.3.</span> <span class="toc-text">第3关：手写数字识别</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%BB%E5%8A%A1%E6%8F%8F%E8%BF%B0-2"><span class="toc-number">1.3.1.</span> <span class="toc-text">任务描述</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%9B%B8%E5%85%B3%E7%9F%A5%E8%AF%86-2"><span class="toc-number">1.3.2.</span> <span class="toc-text">相关知识</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E7%AE%80%E4%BB%8B"><span class="toc-number">1.3.2.1.</span> <span class="toc-text">数据简介</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#RandomForestClassifier"><span class="toc-number">1.3.2.2.</span> <span class="toc-text">RandomForestClassifier</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BC%96%E7%A8%8B%E8%A6%81%E6%B1%82-2"><span class="toc-number">1.3.3.</span> <span class="toc-text">编程要求</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%8B%E8%AF%95%E8%AF%B4%E6%98%8E-2"><span class="toc-number">1.3.4.</span> <span class="toc-text">测试说明</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%82%E8%80%83%E7%AD%94%E6%A1%88-2"><span class="toc-number">1.3.5.</span> <span class="toc-text">参考答案</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2025/01/05/other-%E9%9A%8F%E7%AC%94-icarus-%E4%B8%BB%E9%A2%98%E5%AE%89%E8%A3%85/" title="Hexo 配置 Icarus 主题"><img src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-4.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Hexo 配置 Icarus 主题"/></a><div class="content"><a class="title" href="/2025/01/05/other-%E9%9A%8F%E7%AC%94-icarus-%E4%B8%BB%E9%A2%98%E5%AE%89%E8%A3%85/" title="Hexo 配置 Icarus 主题">Hexo 配置 Icarus 主题</a><time datetime="2025-01-05T08:25:00.000Z" title="发表于 2025-01-05 16:25:00">2025-01-05</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2024/03/18/other-leetcode-%E3%80%902024-03-18-LeetCode-%E5%88%B7%E9%A2%98-400%E9%81%93%E6%89%93%E5%8D%A1%E3%80%91/" title="LeetCode刷题400道打卡"><img src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-9.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="LeetCode刷题400道打卡"/></a><div class="content"><a class="title" href="/2024/03/18/other-leetcode-%E3%80%902024-03-18-LeetCode-%E5%88%B7%E9%A2%98-400%E9%81%93%E6%89%93%E5%8D%A1%E3%80%91/" title="LeetCode刷题400道打卡">LeetCode刷题400道打卡</a><time datetime="2024-03-17T16:00:00.000Z" title="发表于 2024-03-18 00:00:00">2024-03-18</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2024/01/04/2024-01-05%20%E5%A4%9A%E6%A8%A1%E6%80%81%E6%A8%A1%E5%9E%8B%E7%BB%BC%E8%BF%B0/" title="多模态基础模型：从专家到通用助理"><img src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-7.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="多模态基础模型：从专家到通用助理"/></a><div class="content"><a class="title" href="/2024/01/04/2024-01-05%20%E5%A4%9A%E6%A8%A1%E6%80%81%E6%A8%A1%E5%9E%8B%E7%BB%BC%E8%BF%B0/" title="多模态基础模型：从专家到通用助理">多模态基础模型：从专家到通用助理</a><time datetime="2024-01-04T14:42:08.000Z" title="发表于 2024-01-04 22:42:08">2024-01-04</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/12/30/%E6%A0%A1%E5%9B%AD%E7%BD%91%E8%87%AA%E5%8A%A8%E7%99%BB%E5%BD%95%E8%84%9A%E6%9C%AC/" title="校园网自动登录脚本"><img src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-9.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="校园网自动登录脚本"/></a><div class="content"><a class="title" href="/2023/12/30/%E6%A0%A1%E5%9B%AD%E7%BD%91%E8%87%AA%E5%8A%A8%E7%99%BB%E5%BD%95%E8%84%9A%E6%9C%AC/" title="校园网自动登录脚本">校园网自动登录脚本</a><time datetime="2023-12-30T09:21:30.000Z" title="发表于 2023-12-30 17:21:30">2023-12-30</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/11/24/%E5%A4%9A%E6%A8%A1%E6%80%81%E8%AE%BD%E5%88%BA%E8%AF%86%E5%88%AB%E5%9F%BA%E7%BA%BF%E6%A8%A1%E5%9E%8B%E5%A4%8D%E7%8E%B0/" title="多模态讽刺识别基线模型复现"><img src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-10.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="多模态讽刺识别基线模型复现"/></a><div class="content"><a class="title" href="/2023/11/24/%E5%A4%9A%E6%A8%A1%E6%80%81%E8%AE%BD%E5%88%BA%E8%AF%86%E5%88%AB%E5%9F%BA%E7%BA%BF%E6%A8%A1%E5%9E%8B%E5%A4%8D%E7%8E%B0/" title="多模态讽刺识别基线模型复现">多模态讽刺识别基线模型复现</a><time datetime="2023-11-24T08:51:18.000Z" title="发表于 2023-11-24 16:51:18">2023-11-24</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2022 - 2025 By David</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener external nofollow noreferrer" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener external nofollow noreferrer" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><a href="http://www.beian.gov.cn/portal/registerSystemInfo" rel="external nofollow noreferrer" target="_blank"> <img style="vertical-align:middle; width:20px; " src="https://cdn.jsdelivr.net/gh/David-deng-01/images/blog/icp.png"> 赣公网安备36082302000135号</a> <a href="https://beian.miit.gov.cn/" rel="external nofollow noreferrer" id="beian"  target="_blank">赣ICP备2023013705号-1</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="日间和夜间模式切换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><div class="js-pjax"></div><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-nest.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>